<?php

namespace common\behavior\CommonMessagePushing;


use common\model\EntityModel;
use common\model\TaskModel;
use common\model\UserModel;
use common\model\VersionCheckListModel;
use common\service\BehaviorService;
use common\service\VersionService;
use Webman\Stomp\Client;

/**
 * 延期单待确认 消息通知
 */
class TaskReviewActiveNodeOutsourceBehavior
{
    /**
     * @param $params
     * @return void
     */
    public function run($params)
    {
        /***********  预处理开始  ************/
        $params = BehaviorService::getInstance()->dealParam(
            $params,
            [
                'version' => '',
                'review_request' => '',
                'nodes' => '',
            ],
            [
                'version' => [],
                'review_request' => [],
                'nodes' => [],
            ],
            'data',
            ['type' => 'task_review_active_node_outsource'],
        );
        /***********  行为判断  ************/
        if ($params === false) {
            return;
        }
        /***********  变量迁移  ************/
        list($version, $reviewRequest, $nodes) = $params;
        if (empty($version) || empty($nodes) || empty($reviewRequest)) {
            return;
        }
        /***********  预处理结束  ************/

        /***********  原逻辑迁移  ************/
        $reviewMan = 0;
        $nodeId = 0;
        foreach ($nodes as $node) {
            if ($node['is_pass'] = "no" && $node['is_reviewed'] == "no" && $node['is_active'] == "yes") {
                $reviewMan = $node['plan_reviewer'];
                $nodeId = $node['id'];
                break;
            }
        }
        if ($reviewMan <= 0) {
            return;
        }
        $task = model(TaskModel::class)->find($version['link_id']);
        $tenant = teamones_request('teamones-im', 'tenant/find', ['param' => [
            'filter' => [
                'id' => $task['tenant_id'],
            ],
        ]])['data'];

        $versionCheckList = model(VersionCheckListModel::class)->forceMasterDB()->where(['version_id' => $version['id']])->find();
        if (empty($versionCheckList)) {
            return ;
        }
        $entity = model(EntityModel::class)->field('name')->find($task['entity_id']);
        $messageParam = [
            'first' => '任务已提交',
            'supplier_name' => $tenant['name'],
            'task_name' => $entity['name'] . "-" . $task['name'],
            'create_at' => date("Y/m/d H:i"),
            'remark' => '请及时审核',
            'version_id' => $versionCheckList['version_id'],
            'review_request_id' => $reviewRequest['id'],
            'task_id' => $task['id'],
            'review_request_node_id' => $nodeId,
        ];
        $messageTemplateId = VersionService::getInstance()->getShadowTaskReviewNotifyTemplateId();

        $reviewMan = model(UserModel::class)->field('union_id')->find($reviewMan);

        /***********  发送新的队列  ************/
        Client::send('common_message_pushing', build_queue_data_with_xu([
            'message_generated' => [[
                "param" => $messageParam,
                'receiver' => $reviewMan['union_id'],
                'template_id' => $messageTemplateId,
                'content_type' => 'card_message',
                'notice_type' => 'message_review',
                'is_popup' => 1,
                'message_key' => 'task_version_need_review',
            ]],
        ]));
        /***********  结束  ************/
    }
}
